using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleService
{
    /// <summary>
    /// Common routines
    /// </summary>
    public static class Parameter
    {
        /// <summary>
        /// Returns parameter value
        /// </summary>
        /// <remarks>
        /// <example>
        ///     uriString http://example.com/services/request/calculators/monthlyPayment/rate=0&term=0&amount=0
        ///     string paramValue = Parameter.ParseParam(uriString, "/calculators/");
        /// </example>
        /// Returns 'monthlyPayment'
        /// </remarks>
        /// <param name="_uri"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public static string ParseParam(string _uri, string _key)
        {
            string paramString = string.Empty;
            int paramIndex = _uri.IndexOf(_key, StringComparison.OrdinalIgnoreCase);
            if (paramIndex > -1)
            {
                paramString = _uri.Substring(paramIndex + _key.Length);
            }

            return paramString.Substring(0, paramString.LastIndexOf('/'));
        }

        /// <summary>
        /// Splits Uri Parameters in to an array
        /// </summary>
        /// <example>
        ///     uriString http://example.com/services/request/calculators/monthlyPayment/rate=0&term=0&amount=0
        ///     string[] paramList = Parameter.SplitParam(uriString, "/monthlyPayment/");
        /// 
        /// </example>
        /// <param name="_uri"></param>
        /// <param name="_key"></param>
        /// <returns></returns>
        public static string[] SplitParam(string _uri, string _key)
        {
            string paramString = string.Empty;
            int paramIndex = _uri.IndexOf(_key, StringComparison.OrdinalIgnoreCase);
            if (paramIndex > -1)
            {
                paramString = _uri.Substring(paramIndex + _key.Length);
            }
             return paramString.Split('&');
        }
    }
}

